define([
	'../../service',
	'../../config',
	'text!./header.html',
	'../../widgets/search_widget/search_widget'
	],function(api,config,sourceHTML){
	
		var vm = avalon.define({
			$id: "header",
			navs : [],
			$setting:{},
			$skipArray: ["navs"],
			currentHash : location.hash.replace(/\#\!/g, ''),
			showSearchWidget:function(state){
        		return (state.stateName == "videos"||state.stateName == "weikes"||state.stateName == "albums"||state.stateName == "albums.item"||state.stateName == "onlines")
			},
			showNav:function(state){
				return (state.stateName != "home"&&state.stateName != "liveRegister"&&state.stateName!="myVideos"&&state.stateName !="liveRegister.item"&&state.stateName !="myRegister")
			},
			isCurrentNav:function(state,url){

				return state.url.indexOf(url) == -1?false:true;
			}
		});
		var states = avalon.router.routingTable["get"];
		_.each(config.navs, function(n){ 
			var s = _.filter(states,function(s){
				return s.stateName == n;
			})
			if(s.length) vm.navs.push(s[0]);
			
		})

		window.onhashchange =  function () {
			vm.currentHash = location.hash.replace(/\#\!/g, '')
		}
		

		return avalon.controller(function ($ctrl) {
		    // 视图渲染后，意思是avalon.scan完成
		    $ctrl.$onRendered = function () {

		      
		    };
		    // 进入视图
		    $ctrl.$onEnter = function (param, rs, rj) {
		    	return api.getItems({
		    		method:api.getSetting
		    	},function(res){
		    		console.log(vm.currentHash)
		    		if(!vm.$setting)	vm.$setting = {};
		    		vm.$setting = res.data;
		    		document.title = vm.$setting.systemName;
		    		if(vm.$setting.isGray)
		    			$("html").addClass("grayIcon")
		    		if(!avalon.vmodels.app.$setting)	avalon.vmodels.app.$setting = {}
		    		avalon.vmodels.app.$setting = res.data
		    		vm.$fire("all!setting",vm.$setting)

		    		setTimeout(function(){
		    			$(window).trigger("imagesDefault")
		    		},100)
		    	})

		    };
		    // 对应的视图销毁前
		    $ctrl.$onBeforeUnload = function () {

		    }; 
		    $ctrl.$vmodels = [vm];
		}) 
})